package party.damao.issue.common;

import java.io.File;
import java.util.Calendar;
import java.util.Random;
import java.util.UUID;

public class Utils {


    public static String get32UUID() {
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        return uuid;
    }

    /**
     * 获取文件后缀名
     *
     * @param fileName
     * @return
     */
    public static String getExt(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }


    /**
     * 获取文件名，不包含后缀
     *
     * @param fileName
     * @return
     */
    public static String getPre(String fileName) {
        return fileName.substring(0, fileName.lastIndexOf("."));
    }

    /**
     * 生成指定长度的随机字符串，为数字和大小写字符
     *
     * @param length 指定生成字符串的长度
     * @return
     */
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer buf = new StringBuffer();

        for (int i = 0; i < length; i++) {
            int num = random.nextInt(62);
            buf.append(str.charAt(num));
        }

        return buf.toString();
    }


    /**
     * @param fileName 原文件名
     * @return
     */
    public static String getNewFileName(String fileName) {
        String lastName = getExt(fileName);
        StringBuffer buffer = new StringBuffer();
        buffer.append(System.currentTimeMillis()).append(getRandomString(4)).append(".").append(lastName);
        return buffer.toString();
    }

    /**
     * 根据当前日期生成目录结构，格式为：年-月/日/
     *
     * @return
     */
    public static String geneFilePathByDate() {
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int mon = (now.get(Calendar.MONTH) + 1);
        int day = now.get(Calendar.DAY_OF_MONTH);
        return year + "_" + mon + File.separator + day + File.separator;

    }

}